﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using StockLib;

namespace StockNet
{
    public class StockReceiverDriver
    {
        private const int RCV_WORK_SENDMSG = 4;      // 工作方式类型定义-窗口消息方式

        private const int RCV_DATA_MSG = 0x8001;     // 指定使用的消息

        private static StockLib.Utils.DllUtils _dllInvoker = new StockLib.Utils.DllUtils();

        public static bool LoadDriver()
        {
            return _dllInvoker.LoadDll("Stock.dll");
        }

        public static bool UnloadDriver()
        {
            return _dllInvoker.UnloadDll();
        }

        public static bool Init(IntPtr hWnd)
        {
            DelegateStockInit func = (DelegateStockInit)_dllInvoker.Invoke("Stock_Init", typeof(DelegateStockInit));

            return func(hWnd, RCV_DATA_MSG, RCV_WORK_SENDMSG) > 0;
        }

        public static bool Quit(IntPtr hWnd)
        {
            DelegateStockQuit func = (DelegateStockQuit)_dllInvoker.Invoke("Stock_Quit", typeof(DelegateStockQuit));
            return func(hWnd) > 0;
        }

        public static bool Setup(bool bHide)
        {
            DelegateSetupReceiver func = (DelegateSetupReceiver)_dllInvoker.Invoke("SetupReceiver", typeof(DelegateSetupReceiver));
            return func(Convert.ToInt32(bHide)) > 0;
        }

        public static string GetDriverInfo(int nType)
        {
            StringBuilder info = new StringBuilder();

            DelegateGetStockDriverInfo func = (DelegateGetStockDriverInfo)_dllInvoker.Invoke("GetStockDriverInfo", typeof(DelegateGetStockDriverInfo));
            func(nType, info);

            return info.ToString();
        }

        private delegate int DelegateStockInit(IntPtr hWnd, UInt32 msg, int workMode);

        private delegate int DelegateStockQuit(IntPtr hWnd);

        private delegate int DelegateSetupReceiver(int nHide);

        private delegate int DelegateGetStockDriverInfo(int nType, StringBuilder strInfo);
    }
}
